package com.hwhhhh.mygrad;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.TypedValue;
import android.widget.LinearLayout;

import com.google.gson.reflect.TypeToken;
import com.hwhhhh.mygrad.adapter.HistoryAdapter;
import com.hwhhhh.mygrad.api.Api;
import com.hwhhhh.mygrad.api.ApiCallback;
import com.hwhhhh.mygrad.api.ApiConfig;
import com.hwhhhh.mygrad.decoration.HistoryItemDecoration;
import com.hwhhhh.mygrad.dto.AppointDTO;
import com.hwhhhh.mygrad.dto.UserDTO;
import com.hwhhhh.mygrad.entity.RespData;
import com.hwhhhh.mygrad.entity.RespDataArr;
import com.hwhhhh.mygrad.utils.StringUtils;

public class HistoryActivity extends BaseActivity {
    private RecyclerView rvHistory;
    private UserDTO user;

    @Override
    protected void initView() {
        rvHistory = findViewById(R.id.rv_history);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_history;
    }

    @Override
    protected void initData() {
        Bundle bundle = getIntent().getExtras();
        user = (UserDTO) bundle.get("user");
        requestData();
    }

    private void requestData() {
        Api.config(ApiConfig.HISTORY, null).getRequest(this, new ApiCallback() {
            @Override
            public void onSuccess(String res) {
                RespDataArr<AppointDTO> data = StringUtils.fromJsonArr(res, new TypeToken<RespDataArr<AppointDTO>>(){}.getType());
                if (data.getCode() == 200) {
                    HistoryActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            HistoryAdapter adapter = new HistoryAdapter(data.getData());
                            LinearLayoutManager layoutManager = new LinearLayoutManager(HistoryActivity.this);
                            HistoryItemDecoration itemDecoration = new HistoryItemDecoration(HistoryActivity.this, getResources().getColor(R.color.divider_color), 1, 1, data.getData());
                            itemDecoration.setTitleHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,35,getResources().getDisplayMetrics()));
                            rvHistory.addItemDecoration(itemDecoration);
                            rvHistory.setLayoutManager(layoutManager);
                            rvHistory.setAdapter(adapter);
                        }
                    });
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }
}